﻿using Newtonsoft.Json;

namespace Fibot.Module.WangDianTong.Base;

abstract class BaseService<GetOutputDto, GetListInputDto, GetListOutputDto>
{
    /// <summary>
    /// 服务名称
    /// </summary>
    public string serviceName = "";

    /// <summary>
    /// 接口参数
    /// </summary>
    public Dictionary<string, object> param;

    /// <summary>
    /// 旺店通配置
    /// </summary>
    public WdtOptions options = new WdtOptions();

    #region 构造器
    /// <summary>
    /// 构造器
    /// </summary>
    protected BaseService()
    {
        param = new Dictionary<string, object>();
    }

    /// <summary>
    /// 构造器
    /// </summary>
    /// <param name="_option"></param>
    protected BaseService(WdtOptions _option)
    {
        param = new Dictionary<string, object>();
        options = _option;
    }
    #endregion

    #region 接口方法
    /// <summary>
    /// 查询方法
    /// </summary>
    /// <returns></returns>
    public virtual GetListOutputDto query(GetListInputDto getListInputDto)
    {
        WdtClient client = new WdtClient(options);
        if (getListInputDto != null)
        {
            param = getListInputDto.ToDictionary();
        }

        double epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
        if (param.ContainsKey("sid"))
        {
            param["sid"] = options.Sid;
        }
        else
        {
            param.Add("sid", options.Sid);
        }
        if (param.ContainsKey("appkey"))
        {
            param["appkey"] = options.Appkey;
        }
        else
        {
            param.Add("appkey", options.Appkey);
        }
        if (param.ContainsKey("timestamp"))
        {
            param["timestamp"] = epoch.ToString("f0");
        }
        else
        {
            param.Add("timestamp", epoch.ToString("f0"));
        }

        string result = client.wdtOpenapi(serviceName, param);
        return JsonConvert.DeserializeObject<GetListOutputDto>(result);
    }
    #endregion
}
